<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>close_socket</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../ref.html" title="This Page Intentionally Left Blank 2/2">
<link rel="prev" href="boost__beast__beast_close_socket.html" title="beast_close_socket">
<link rel="next" href="boost__beast__operator_plus_.html" title="operator+">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost__beast__beast_close_socket.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost__beast__operator_plus_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="beast.ref.boost__beast__close_socket"></a><a class="link" href="boost__beast__close_socket.html" title="close_socket">close_socket</a>
</h4></div></div></div>
<p>
        <a class="indexterm" name="idm69982"></a>
      </p>
<p>
        Close a socket or socket-like object.
      </p>
<h5>
<a name="beast.ref.boost__beast__close_socket.h0"></a>
        <span class="phrase"><a name="beast.ref.boost__beast__close_socket.synopsis"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.synopsis">Synopsis</a>
      </h5>
<p>
        Defined in header <code class="literal">&lt;<a href="../../../../../../boost/beast/core/stream_traits.hpp" target="_top">boost/beast/core/stream_traits.hpp</a>&gt;</code>
      </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">Socket</span><span class="special">&gt;</span>
<span class="keyword">void</span>
<span class="identifier">close_socket</span><span class="special">(</span>
    <span class="identifier">Socket</span><span class="special">&amp;</span> <span class="identifier">sock</span><span class="special">);</span>
</pre>
<h5>
<a name="beast.ref.boost__beast__close_socket.h1"></a>
        <span class="phrase"><a name="beast.ref.boost__beast__close_socket.description"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.description">Description</a>
      </h5>
<p>
        This function attempts to close an object representing a socket. In this
        context, a socket is an object for which an unqualified call to the function
        <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">beast_close_socket</span><span class="special">(</span><span class="identifier">Socket</span><span class="special">&amp;)</span></code> is well-defined. The function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code> is a <span class="emphasis"><em>customization
        point</em></span>, allowing user-defined types to provide an algorithm for
        performing the close operation by overloading this function for the type
        in question. Since the customization point is a function call, the normal
        rules for finding the correct overload are applied including the rules for
        argument-dependent lookup ("ADL"). This permits classes derived
        from a type for which a customization is provided to inherit the customization
        point. An overload for the networking class template <code class="computeroutput"><span class="identifier">net</span><span class="special">::</span><span class="identifier">basic_socket</span></code>
        is provided, which implements the close algorithm for all socket-like objects
        (hence the name of this customization point). When used in conjunction with
        <a class="link" href="boost__beast__get_lowest_layer.html" title="get_lowest_layer"><code class="computeroutput"><span class="identifier">get_lowest_layer</span></code></a>,
        a generic algorithm operating on a layered stream can perform a closure of
        the underlying socket without knowing the exact list of concrete types.
      </p>
<h5>
<a name="beast.ref.boost__beast__close_socket.h2"></a>
        <span class="phrase"><a name="beast.ref.boost__beast__close_socket.example_1"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.example_1">Example
        1</a>
      </h5>
<p>
        The following generic function synchronously sends a message on the stream,
        then closes the socket.
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">WriteStream</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">hello_and_close</span> <span class="special">(</span><span class="identifier">WriteStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">net</span><span class="special">::</span><span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">net</span><span class="special">::</span><span class="identifier">const_buffer</span><span class="special">(</span><span class="string">"Hello, world!"</span><span class="special">,</span> <span class="number">13</span><span class="special">));</span>
    <span class="identifier">close_socket</span><span class="special">(</span><span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">stream</span><span class="special">));</span>
<span class="special">}</span>
</pre>
<p>
        To enable closure of user defined types, it is necessary to provide an overload
        of the function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code>
        for the type.
      </p>
<h5>
<a name="beast.ref.boost__beast__close_socket.h3"></a>
        <span class="phrase"><a name="beast.ref.boost__beast__close_socket.example_2"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.example_2">Example
        2</a>
      </h5>
<p>
        The following code declares a user-defined type which contains a private
        socket, and provides an overload of the customization point which closes
        the private socket.
      </p>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">my_socket</span>
<span class="special">{</span>
    <span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">sock_</span><span class="special">;</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">my_socket</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">io_context</span><span class="special">&amp;</span> <span class="identifier">ioc</span><span class="special">)</span>
        <span class="special">:</span> <span class="identifier">sock_</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">)</span>
    <span class="special">{</span>
    <span class="special">}</span>

    <span class="keyword">friend</span> <span class="keyword">void</span> <span class="identifier">beast_close_socket</span><span class="special">(</span><span class="identifier">my_socket</span><span class="special">&amp;</span> <span class="identifier">s</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
        <span class="identifier">s</span><span class="special">.</span><span class="identifier">sock_</span><span class="special">.</span><span class="identifier">close</span><span class="special">(</span><span class="identifier">ec</span><span class="special">);</span>
        <span class="comment">// ignore the error</span>
    <span class="special">}</span>
<span class="special">};</span>
</pre>
<h5>
<a name="beast.ref.boost__beast__close_socket.h4"></a>
        <span class="phrase"><a name="beast.ref.boost__beast__close_socket.parameters"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.parameters">Parameters</a>
      </h5>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody><tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">sock</span></code>
                </p>
              </td>
<td>
                <p>
                  The socket to close. If the customization point is not defined
                  for the type of this object, or one of its base classes, then a
                  compiler error results.
                </p>
              </td>
</tr></tbody>
</table></div>
<h5>
<a name="beast.ref.boost__beast__close_socket.h5"></a>
        <span class="phrase"><a name="beast.ref.boost__beast__close_socket.see_also"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.see_also">See
        Also</a>
      </h5>
<p>
        <a class="link" href="boost__beast__beast_close_socket.html" title="beast_close_socket"><code class="computeroutput"><span class="identifier">beast_close_socket</span></code></a>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost__beast__beast_close_socket.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost__beast__operator_plus_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
